/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controladores;

import Beans.Pais;
import Beans.Provincia;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Ryodans
 */
public class MUbicacion {
private  List <Pais> Paises = new LinkedList<Pais>();//lista

  Pais p = new Pais();
   File archivo=new File("Pais.dat");

    public List<Pais> getPaises() {
        return Paises;
    }

    public void setPaises(List<Pais> Paises) {
        this.Paises = Paises;
    }


     public void grabar() throws IOException {

      FileOutputStream   flujoSalida  = new FileOutputStream(archivo);
      ObjectOutputStream objetoSalida = new ObjectOutputStream(flujoSalida);
      objetoSalida.writeObject(this.getPaises());
      objetoSalida.close();
     }




    public void restaurar() throws IOException,ClassNotFoundException {
    if(archivo.exists()==true){
        FileInputStream flujoEntrada    = new FileInputStream(archivo);
  	ObjectInputStream objetoEntrada = new ObjectInputStream(flujoEntrada);
  	List<Pais> pa = (List) objetoEntrada.readObject();
  	this.setPaises(pa);
        objetoEntrada.close();
    }

    }

      public Boolean Existe(String nombre){
        boolean sw=false;
        Iterator it = getPaises().iterator();
        while(it.hasNext()){
            p = (Pais)it.next();
            if(p.getNombre().equals(nombre)){
                sw=true;
            }
        }
        return sw;
    }

      public List lista(String nombre, List<Provincia> s){
           Iterator itc=s.iterator();
         while(itc.hasNext()){
             Provincia pse = (Provincia)itc.next();
            if(pse.getNombre().equals(nombre)){
                return pse.getCiudad();
            }
        }
          return null;
      }
      public void Modificar(int pos,String codigo,Pais country){
       Iterator itc=getPaises().iterator();
         while(itc.hasNext()){
             p = (Pais)itc.next();
            if(p.getNombre().equals(codigo)){
                getPaises().set(pos, country);
                JOptionPane.showMessageDialog(null,"Los datos se guardaron "
                                                 + "Satisfactoriamente");
            }
        }
       }

      public int Posicion(String nombre){
          Iterator itc=getPaises().iterator();
          int pos=-1;

         while(itc.hasNext()){
             p = (Pais)itc.next();
            if(p.getNombre().compareTo(nombre)>0){
                pos = pos+1;
            }
      }
          if(pos!=-1){
              pos = pos+1;
          }
          return pos;
      }

      public void Modificar(String nombre,Pais country){
       Iterator itc=getPaises().iterator();
         int posicion = this.Posicion(nombre);
         getPaises().set(posicion, country);
         JOptionPane.showMessageDialog(null,"Los datos se guardaron "
                                                 + "Satisfactoriamente");
       }

     public void Registrar(Pais pr){
         if(!Existe(pr.getNombre())){
             getPaises().add(pr);
             JOptionPane.showMessageDialog(null,"El Pais se registro "
                                              + "correctamente");
         }
        try {
            this.grabar();
        } catch (IOException ex) {
            Logger.getLogger(MUbicacion.class.getName()).log(Level.SEVERE, null, ex);
        }

      }

    public int GenerarCodigo(){
        int codigo= 0;
        int limite = 10000;
        Random r = new Random();
        codigo = r.nextInt(limite+1);
       return codigo;

    }
    public Pais Consultar(String nombre){
        Iterator it = getPaises().iterator();
        while(it.hasNext()){
            Pais pro = (Pais)it.next();
              if(pro.getNombre().equals(nombre)){
                 return pro;
              }
         }
        return new Pais();
     }
    

     public void Eliminar(String nit){
        try{
        Iterator it = getPaises().iterator();
        while(it.hasNext()){
        Pais pr =(Pais)it.next();
        if(pr.getNombre().equals(nit)){
          getPaises().remove(pr);
                    try {
                        this.grabar();
                    } catch (IOException ex) {
                        Logger.getLogger(MUbicacion.class.getName()).log(Level.SEVERE, null, ex);
                    }
              JOptionPane.showMessageDialog(null,"Eliminado","Informacion",JOptionPane.INFORMATION_MESSAGE);
            }
         }
         }catch(ConcurrentModificationException e){

         }
     }
}
